<?php
class UsersController extends AppController {

	var $name = 'Users';
	var $helpers = array('Html', 'Form');
	var $components = array('InheritAcl');
	var $uses = array('User', 'Ballot');

	function beforeFilter() {
		//login and logout action are allowed to anyone
		$this->Auth->allow('login', 'logout', 'register');
		$this->Auth->autoRedirect = false;
		parent::beforeFilter();
	}

	function login() {
		if (empty($this->data)) {
			$this->data = $this->Cookie->read('Auth.User');
		}
		
		if(!empty($this->data)) {
			if($this->Auth->login()) {
				/*
				$user = $this->User->find('first', $this->Session->read('Auth.User.id'));
				$this->Session->write('user', $user);
				
				if (isset($user['PersonalProfile'])) {
					$interests = explode(',', $user['PersonalProfile']['political_interests']);
					if (!empty($interests)) {
						$this->Session->write('interests.personal_political_interests', $interests);
					}
				}
				*/
				
				$cookie = array();
				$cookie['User']['username'] = $this->data['User']['username'];
				$cookie['User']['password'] = $this->data['User']['password'];
				if ($this->data['User']['remember_me']) {
					$this->Cookie->write('Auth.User', $cookie, true, '+2 weeks');
				}
				unset($this->data['User']['remember_me']);

				$ballot = $this->Ballot->findLastestBallot($this->Session->read('Auth.User.id'));
				if (!empty($ballot)) {
					$ballotData = $ballot['Ballot']['ballot_data'];
					$this->Session->write('ballotData', $ballotData);
					$this->set('ballot_id', $ballot['Ballot']['id']);
					$this->set('ballotData', $ballotData);
				}
				$this->Session->setFlash('Welcome '.$this->Auth->user('username'));
			}
			else {
				$this->Session->setFlash('Login incorrect!');
			}
      $this->redirect($this->Auth->redirect());
		}

	}

	function logout() {
		$this->Cookie->del('Auth.User');
		$username = $this->Auth->user('username');
		$this->Auth->logout();
		$this->Session->destroy();
		$this->Session->setFlash('Goodbye '.$username);
		$this->redirect('/', null, true);
	}

	function index() {
		$this->User->recursive = 0;
		$this->set('users', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid User.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('user', $this->User->read(null, $id));
	}

	function register() {
		if (!empty($this->data)) {
			if ($this->data['User']['password'] ==
			$this->Auth->password($this->data['User']['password_confirm'])) {
				$this->User->create();
				$this->data['User']['role_id'] = 2;
				if ($this->User->save($this->data)) {
					$this->Session->setFlash(__('Registeration succeeded.', true));
					$this->login();
				} else {
					$this->Session->setFlash(__('The User could not be saved. Please, try again.', true));
				}
			} else {
				$this->flash('Typed passwords did not match', 'users/register');
			}
		}
		$roles = $this->User->Role->find('list');
		$this->set(compact('roles'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid User', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->User->save($this->data, true, array('role_id'))) {
				// we might have to reset the parent aro
				$this->InheritAcl->checkAroParent('User', $this->data['User']['id'], 'Role', $this->data['User']['role_id']);
				$this->Session->setFlash(__('The User has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The User could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->User->read(null, $id);
		}
		$roles = $this->User->Role->find('list');
		$this->set(compact('roles'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for User', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->User->del($id)) {
			$this->Session->setFlash(__('User deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

}
?>